import "../g_vars";

import WBinaClient from "../bina/WBinaClient";
import storage from '../db/index';
import config from '../config';
import { decryptAes } from '../utils/crypto-tools';

import yargs  from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';

const argv:any = yargs(hideBin(process.argv))
    .option("symbol", {alias:'s', type: 'string'})
    .option("id", {type: 'string'})
    .option("key", {type: 'string'})
    .option("iv", {type: 'string'})
    .option("proxyswitch", {alias: 'w', type: 'boolean'})
    .parse()

let symbol = argv.s;
let id = argv.id;
let key = argv.key;
let iv=argv.iv;
let w = argv.w;
if(!symbol || !id || !key || !iv || !w) {
    console.log('please use s, id, key, iv, w');
    process.exit(1);
}
console.log("symbol, id, key, iv, pw", symbol, id, key, iv, w);

const main = async() => {
    let config_id = config.bina.account.id + config.bina.account.id2 + "==";
    let config_key = config.bina.account.key + config.bina.account.key2 + '==';

    let apiKey = decryptAes(iv, config_id, id);
    let apiSecret = decryptAes(iv, config_key, key);



    let binaClient = new WBinaClient(w, apiKey, apiSecret);
    await binaClient.createClient();

    let myTrades = await binaClient.getMyTrades(symbol);

    let mytradeModel = storage.getMyTrade();
    for(let i=0; i< myTrades.length;i++) {
        let trade = myTrades[i];
        console.log('trade: ', trade);
        trade.symbol = symbol;
        if(mytradeModel){
            await mytradeModel.insertTrade(trade);
        }else{
            console.log('mytrade is null');
        }

    }
    console.log('finish get trade!');


}
main()